Imports System.IO
Imports System.Text
Imports System.Management
Imports System.Security.Cryptography
Imports System.Net.NetworkInformation
Public Class o3DES
    Private m_des As New TripleDESCryptoServiceProvider
    Public Shared ReadOnly key() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
    Public Shared ReadOnly iv() As Byte = {8, 7, 6, 5, 4, 3, 2, 1}
    Public Shared DES As New o3DES(key, iv)
    ' define the string handler
    Private m_utf8 As New UTF8Encoding

    ' define the local property arrays
    Private m_key() As Byte
    Private m_iv() As Byte

    Public Sub New(ByVal key() As Byte, ByVal iv() As Byte)
        Me.m_key = key
        Me.m_iv = iv
    End Sub
    Public Function Encrypt(ByVal input() As Byte) As Byte()
        Return Transform(input, m_des.CreateEncryptor(m_key, m_iv))
    End Function
    Public Function Decrypt(ByVal input() As Byte) As Byte()
        Return Transform(input, m_des.CreateDecryptor(m_key, m_iv))
    End Function
    Public Function Encrypt(ByVal text As String) As String
        Dim input() As Byte = m_utf8.GetBytes(text)
        Dim output() As Byte = Transform(input, _
                        m_des.CreateEncryptor(m_key, m_iv))
        Return Convert.ToBase64String(output)
    End Function
    Public Function Decrypt(ByVal text As String) As String
        Dim input() As Byte = Convert.FromBase64String(text)
        Dim output() As Byte = Transform(input, _
                         m_des.CreateDecryptor(m_key, m_iv))
        Return m_utf8.GetString(output)
    End Function
    Private Function Transform(ByVal input() As Byte, _
        ByVal CryptoTransform As ICryptoTransform) As Byte()
        ' create the necessary streams
        Dim memStream As MemoryStream = New MemoryStream
        Dim cryptStream As CryptoStream = New _
            CryptoStream(memStream, CryptoTransform, _
            CryptoStreamMode.Write)
        ' transform the bytes as requested
        cryptStream.Write(input, 0, input.Length)
        cryptStream.FlushFinalBlock()
        ' Read the memory stream and convert it back into byte array
        memStream.Position = 0
        Dim result(CType(memStream.Length - 1, System.Int32)) As Byte
        memStream.Read(result, 0, CType(result.Length, System.Int32))
        ' close and release the streams
        memStream.Close()
        cryptStream.Close()
        ' hand back the encrypted buffer
        Return result
    End Function
    Public Shared Function GetFirstMACAddress() As String
        Dim mc As System.Management.ManagementClass
        Dim mo As Management.ManagementObject
        Dim mac As String = ""
        mc = New Management.ManagementClass("Win32_NetworkAdapterConfiguration")
        Dim moc As Management.ManagementObjectCollection = mc.GetInstances()
        For Each mo In moc
            If mo.Item("IPEnabled") = True Then
                mac = mo.Item("MacAddress").ToString()
                Exit For
            End If
        Next
        Return mac
    End Function
    Public Shared Sub GenerateREGFile(ByVal FileName As String)
        Dim NewFile As File
        Dim oWrite As StreamWriter
        Dim MacAddress As String = GetFirstMACAddress()
        oWrite = NewFile.CreateText(FileName)

        Dim tempstr As String = ""
        Dim tempbyte(0) As Byte
        Dim rd As New RNGCryptoServiceProvider()

        Dim randomArray(255) As Byte
        Dim randomString As String

        rd.GetBytes(randomArray)
        randomString = Convert.ToBase64String(randomArray)

        ' for sure that length of randomstring is 34
        If randomString.Length < 34 Then
            For i As Byte = 1 To 34 - randomString.Length
                randomString = "0" & randomString
            Next
        ElseIf randomString.Length > 34 Then
            randomString = randomString.Substring(1, 34)
        End If
        'MessageBox.Show(randomString.Length)

        Dim MACEn As String
        MACEn = DES.Encrypt(MacAddress)
        'MessageBox.Show(dataen.Length)
        tempstr = tempstr & randomString & MACEn
        For i As Integer = 0 To 1111
            rd.GetBytes(tempbyte)
            tempstr = tempstr & tempbyte(0).ToString
        Next
        oWrite.Write(tempstr)
        oWrite.Close()
    End Sub
    Public Shared Function CheckLicense(ByVal FileName As String) As Boolean
        Dim oFile As File
        Dim oRead As StreamReader
        oRead = oFile.OpenText(FileName)
        Dim tempstr As String
        tempstr = oRead.ReadToEnd
        oRead.Close()
        Dim MAC As String = DES.Decrypt(tempstr.Substring(34, 32))

        oRead.Close()

        'Dim mc As System.Management.ManagementClass
        'Dim mo As Management.ManagementObject
        'Dim mac As String = ""
        Dim ReadMAC As String
        'mc = New Management.ManagementClass("Win32_NetworkAdapterConfiguration")
        'Dim moc As Management.ManagementObjectCollection = mc.GetInstances()
        For Each nic As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces
            'If (nic.OperationalStatus = OperationalStatus.Up) Then
            ReadMAC = nic.GetPhysicalAddress().ToString()
            If MAC.Replace(":", "") = ReadMAC Then
                Return True
            End If
            'End If
            Console.WriteLine(nic.GetPhysicalAddress().ToString())
        Next
        'For Each mo In moc
        '    'If mo.Item("IPEnabled") = True Then
        '    ReadMAC = mo.Item("MacAddress").ToString()
        '    If MAC = ReadMAC Then
        '        Return True
        '    End If
        '    'End If
        'Next

        'Dim ReadMAC As String = GetFirstMACAddress()
        ' If MAC = ReadMAC Then
        'Return True
        'Else
        Return False
        ' End If
    End Function
    
End Class
